﻿using System;
using System.IO;

namespace NGCDS {
    class FileUtil {

        public void RecurseFileTime(string DirectoryPath, DateTime fileTime) {
            // Set the date/time for each sub directory
            foreach (string subDirectory in Directory.GetDirectories(DirectoryPath)) {
                // Loop through each sub-sub directory
                RecurseFileTime(subDirectory, fileTime);

                // Set the date/time for the sub directory
                Directory.SetCreationTime(subDirectory, fileTime);
                Directory.SetLastWriteTime(subDirectory, fileTime);
                Directory.SetLastAccessTime(subDirectory, fileTime);
            }
            // Set the date/time for each file
            foreach (string fileName in Directory.GetFiles(DirectoryPath)) {
                File.SetCreationTime(fileName, fileTime);
                File.SetLastWriteTime(fileName, fileTime);
                File.SetLastAccessTime(fileName, fileTime);
            }
        }

        public void setFileTime(string fileName, DateTime fileTime) {
            if (File.Exists(fileName)) {
                // Select the time for the file
                File.SetCreationTime(fileName, fileTime);
                File.SetLastWriteTime(fileName, fileTime);
                File.SetLastAccessTime(fileName, fileTime);
            }
        }

        public void Copying(Stream FromStream, string TargetFile) {

            // FromStream=the stream we wanna save to a file 
            //TargetFile = name&path of file to be created to save to 
            //i.e"c:\mysong.mp3" 

            try {
                //Creat a file to save to

                Stream ToStream = File.Create(TargetFile);

                //use the binary reader & writer because
                //they can work with all formats
                //i.e images, text files ,avi,mp3..
                BinaryReader br = new BinaryReader(FromStream);
                BinaryWriter bw = new BinaryWriter(ToStream);

                //copy data from the FromStream to the outStream
                //convert from long to int 
                bw.Write(br.ReadBytes((int)FromStream.Length));
                //save
                bw.Flush();
                //clean up 
                bw.Close();
                br.Close();
            }
                //use Exception e as it can handle any exception 
            catch (Exception e) {
                //code if u like 
                Console.WriteLine(e.StackTrace);
            }
        }


    }
}
